package com.ruoyi.report_sj.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.ruoyi.common.utils.BigDecimalUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.report_ljlm.domain.bo.LmysdbgBsInfoBo;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import com.ruoyi.report_tg.domain.TgsybgBaDetail;
import com.ruoyi.report_tg.domain.vo.TgsybgBaDetailVo;
import com.ruoyi.report_tg.mapper.TgsybgBaDetailMapper;
import com.ruoyi.sycs.service.IBiQualificationService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_sj.domain.bo.ReportSjSjbhwxnsyjcbgBsInfoBo;
import com.ruoyi.report_sj.domain.vo.ReportSjSjbhwxnsyjcbgBsInfoVo;
import com.ruoyi.report_sj.domain.ReportSjSjbhwxnsyjcbgBsInfo;
import com.ruoyi.report_sj.mapper.ReportSjSjbhwxnsyjcbgBsInfoMapper;
import com.ruoyi.report_sj.service.IReportSjSjbhwxnsyjcbgBsInfoService;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;
import java.util.stream.Collectors;

/**
 * 砂浆拌合物性能试验检测报告Service业务层处理
 *
 * @author zxs
 * @date 2024-07-09
 */
@RequiredArgsConstructor
@Service
public class ReportSjSjbhwxnsyjcbgBsInfoServiceImpl implements IReportSjSjbhwxnsyjcbgBsInfoService {

    private final ReportSjSjbhwxnsyjcbgBsInfoMapper baseMapper;
    private final TgsybgBaDetailMapper detailMapper;

    private final IBiQualificationService biQualificationService;

    private final static Long[] keys = {155L,156L,157L,158L};

    /**
     * 查询砂浆拌合物性能试验检测报告
     */
    @Override
    @FIllBasicInfo
    public ReportSjSjbhwxnsyjcbgBsInfoVo queryById(Long excelId){
        ReportSjSjbhwxnsyjcbgBsInfoVo vo = baseMapper.selectVoById(excelId);
        // 查询detail
        List<TgsybgBaDetailVo> baDetailVos = detailMapper.selectVoList(new LambdaQueryWrapper<TgsybgBaDetail>()
            .eq(TgsybgBaDetail::getExcelId, excelId));
        baDetailVos.forEach(v->{
            v.setQualInfo(biQualificationService.queryById(v.getQualId()));
        });
        // 格式化 技术指标
        biQualificationService.compute(baDetailVos);
        // 转换成map形式
        if(baDetailVos!=null){
            Map<Long, TgsybgBaDetailVo> collect = baDetailVos.stream().collect(Collectors.toMap(TgsybgBaDetailVo::getQualId, v -> v));
            vo.setBaDetailVos(collect);
        }
        return vo;
    }

    /**
     * 查询砂浆拌合物性能试验检测报告列表
     */
    @Override
    public TableDataInfo<ReportSjSjbhwxnsyjcbgBsInfoVo> queryPageList(ReportSjSjbhwxnsyjcbgBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ReportSjSjbhwxnsyjcbgBsInfo> lqw = buildQueryWrapper(bo);
        Page<ReportSjSjbhwxnsyjcbgBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询砂浆拌合物性能试验检测报告列表
     */
    @Override
    public List<ReportSjSjbhwxnsyjcbgBsInfoVo> queryList(ReportSjSjbhwxnsyjcbgBsInfoBo bo) {
        LambdaQueryWrapper<ReportSjSjbhwxnsyjcbgBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ReportSjSjbhwxnsyjcbgBsInfo> buildQueryWrapper(ReportSjSjbhwxnsyjcbgBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ReportSjSjbhwxnsyjcbgBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getPage() != null, ReportSjSjbhwxnsyjcbgBsInfo::getPage, bo.getPage());
        lqw.eq(bo.getTotalPage() != null, ReportSjSjbhwxnsyjcbgBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), ReportSjSjbhwxnsyjcbgBsInfo::getJiancdwmc, bo.getJiancdwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getJilbh()), ReportSjSjbhwxnsyjcbgBsInfo::getJilbh, bo.getJilbh());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcmc()), ReportSjSjbhwxnsyjcbgBsInfo::getGongcmc, bo.getGongcmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), ReportSjSjbhwxnsyjcbgBsInfo::getGongcbwyt, bo.getGongcbwyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpxx()), ReportSjSjbhwxnsyjcbgBsInfo::getYangpxx, bo.getYangpxx());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), ReportSjSjbhwxnsyjcbgBsInfo::getYangpmc, bo.getYangpmc());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), ReportSjSjbhwxnsyjcbgBsInfo::getYangpbh, bo.getYangpbh());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsljzt()), ReportSjSjbhwxnsyjcbgBsInfo::getYangpsljzt, bo.getYangpsljzt());
        lqw.eq(bo.getYangplysj() != null, ReportSjSjbhwxnsyjcbgBsInfo::getYangplysj, bo.getYangplysj());
        lqw.eq(bo.getShiyrq() != null, ReportSjSjbhwxnsyjcbgBsInfo::getShiyrq, bo.getShiyrq());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjwd()), ReportSjSjbhwxnsyjcbgBsInfo::getShiytjwd, bo.getShiytjwd());
        lqw.eq(StringUtils.isNotBlank(bo.getShiytjsd()), ReportSjSjbhwxnsyjcbgBsInfo::getShiytjsd, bo.getShiytjsd());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), ReportSjSjbhwxnsyjcbgBsInfo::getJiancyj, bo.getJiancyj());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), ReportSjSjbhwxnsyjcbgBsInfo::getPandyj, bo.getPandyj());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), ReportSjSjbhwxnsyjcbgBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
        lqw.eq(StringUtils.isNotBlank(bo.getShajzl()), ReportSjSjbhwxnsyjcbgBsInfo::getShajzl, bo.getShajzl());
        lqw.eq(StringUtils.isNotBlank(bo.getJiaobfs()), ReportSjSjbhwxnsyjcbgBsInfo::getJiaobfs, bo.getJiaobfs());
        lqw.eq(bo.getBiaogmdcz() != null, ReportSjSjbhwxnsyjcbgBsInfo::getBiaogmdcz, bo.getBiaogmdcz());
        lqw.eq(bo.getFencd() != null, ReportSjSjbhwxnsyjcbgBsInfo::getFencd, bo.getFencd());
        lqw.eq(bo.getXiacsd() != null, ReportSjSjbhwxnsyjcbgBsInfo::getXiacsd, bo.getXiacsd());
        lqw.eq(bo.getBaosl() != null, ReportSjSjbhwxnsyjcbgBsInfo::getBaosl, bo.getBaosl());
        lqw.eq(bo.getPingjhsl() != null, ReportSjSjbhwxnsyjcbgBsInfo::getPingjhsl, bo.getPingjhsl());
        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), ReportSjSjbhwxnsyjcbgBsInfo::getFujsm, bo.getFujsm());
        lqw.eq(StringUtils.isNotBlank(bo.getJianc()), ReportSjSjbhwxnsyjcbgBsInfo::getJianc, bo.getJianc());
        lqw.eq(StringUtils.isNotBlank(bo.getJil()), ReportSjSjbhwxnsyjcbgBsInfo::getJil, bo.getJil());
        lqw.eq(StringUtils.isNotBlank(bo.getFuh()), ReportSjSjbhwxnsyjcbgBsInfo::getFuh, bo.getFuh());
        lqw.eq(bo.getRiq() != null, ReportSjSjbhwxnsyjcbgBsInfo::getRiq, bo.getRiq());
        lqw.eq(StringUtils.isNotBlank(bo.getShebIds()), ReportSjSjbhwxnsyjcbgBsInfo::getShebIds, bo.getShebIds());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyjIds()), ReportSjSjbhwxnsyjcbgBsInfo::getJiancyjIds, bo.getJiancyjIds());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyjIds()), ReportSjSjbhwxnsyjcbgBsInfo::getPandyjIds, bo.getPandyjIds());
        lqw.eq(StringUtils.isNotBlank(bo.getBgFlag()), ReportSjSjbhwxnsyjcbgBsInfo::getBgFlag, bo.getBgFlag());
        return lqw;
    }

    /**
     * 新增砂浆拌合物性能试验检测报告
     */
    @Override
    @Transactional
    public Boolean insertByBo(ReportSjSjbhwxnsyjcbgBsInfoBo bo) {
        ReportSjSjbhwxnsyjcbgBsInfo add = BeanUtil.toBean(bo, ReportSjSjbhwxnsyjcbgBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
            // 新增详细 多个keys
            ArrayList<TgsybgBaDetail> details = new ArrayList<>(15);
            for (Long key : keys) {
                details.add(new TgsybgBaDetail(add.getExcelId(), key));
            }
            detailMapper.insertBatch(details);
            bo.setExcelId(add.getExcelId());
        }
        return flag;
    }

    /**
     * 修改砂浆拌合物性能试验检测报告
     */
    @Override
    @Transactional
    public Boolean updateByBo(ReportSjSjbhwxnsyjcbgBsInfoBo bo) {
        // 增加对 是否合格 的填充
        fillExcelResult(bo);
        ReportSjSjbhwxnsyjcbgBsInfo update = BeanUtil.toBean(bo, ReportSjSjbhwxnsyjcbgBsInfo.class);
        validEntityBeforeSave(update);
        if (bo.getBaDetailVos() != null) {
            detailMapper.updateBatchById(BeanUtil.copyToList(bo.getBaDetailVos().values(), TgsybgBaDetail.class));
        }
        return baseMapper.updateById(update) > 0;
    }


    private void fillExcelResult(ReportSjSjbhwxnsyjcbgBsInfoBo bo) {
        if (CollUtil.isNotEmpty(bo.getBaDetailVos())) {
            for (TgsybgBaDetailVo detailVo : bo.getBaDetailVos().values()) {
                if ("不符合".equals(detailVo.getJiegpd())) {
                    bo.setExcelResult(2);
                    return;
                }
            }
            bo.setExcelResult(1);
        }
        bo.setExcelResult(0);
    }

    @Override
    public boolean isWell(Long excelId) {
        return baseMapper.selectVoById(excelId).getExcelResult() != 2;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ReportSjSjbhwxnsyjcbgBsInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除砂浆拌合物性能试验检测报告
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
